import { ENUM_CLIP, ENUM_UI } from "../GbzEnum";
import GbzInstance from "../GbzInstance";
import GbzAudioManager from "../manager/GbzAudioManager";
import GbzDataManager from "../manager/GbzDataManager";
import GbzSdkManager from "../manager/GbzSdkManager";
import GbzToastManager from "../manager/GbzToastManager";
import BaseLayer from "./GbzBaselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzTipLayer extends BaseLayer {

    panel: cc.Node = null
    btn_submit: cc.Node = null
    btn_close: cc.Node = null
    skills: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btn_submit = cc.find('btn_submit', this.panel)
        this.btn_submit.on('click', this.onSubmitClick, this)
        this.btn_close = cc.find('btn_close', this.panel)
        this.btn_close.on('click', this.onCloseClick, this)
        this.skills = this.panel.getChildByName('skills')
    }

    onDestroy() {
        this.btn_submit.off('click', this.onSubmitClick, this)
        this.btn_close.off('click', this.onCloseClick, this)
    }

    onEnable() {
        this.zoom(this.panel)
        GbzSdkManager.ins.showCustomRow(true)
        this.rendor()
    }

    onDisable() {
        GbzSdkManager.ins.showCustomRow(false)
    }

    rendor() {
        this.skills.children.forEach((skill, index) => {
            skill.active = index == GbzDataManager.ins.skillIndex
        })
    }

    onSubmitClick() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)

        if (GbzDataManager.ins.skillIndex == 0) {
            if (!GbzInstance.gameMgr.levelComp.getTargetLock()) {
                GbzToastManager.ins.show('拓展孔已全部解锁', { gravity: 'CENTER', bg_color: cc.color(226, 69, 109, 255) })
                GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_TIP, false)
                return
            }
        }

        if (GbzDataManager.ins.skillIndex == 1) {
            if (!GbzInstance.gameMgr.levelComp.hasTargetScew()) {
                GbzToastManager.ins.show('候停区没有螺丝', { gravity: 'CENTER', bg_color: cc.color(226, 69, 109, 255) })
                GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_TIP, false)
                return
            }
        }

        GbzSdkManager.ins.showVideo(async () => {
            GbzInstance.gameMgr.onGameSkill()
            GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_TIP, false)
        }, (msg: string) => {
            GbzToastManager.ins.show(msg, { gravity: 'TOP', bg_color: cc.color(226, 69, 109, 255) })
        })
    }

    onCloseClick() {
        GbzAudioManager.ins.play(ENUM_CLIP.GAME_CLICK)
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_TIP, false)
    }
}
